package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {
    @Reference
    private IStrategyService strategyService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;

    //攻略的首页 commends
    @RequestMapping("")
    public String index(Model model) {
        //commends  //查询前五个
        model.addAttribute("commends", strategyCommendService.getTop5());
        //hotCds  热门推荐排行的显示  前十个
        List<StrategyStatisVO> vos = strategyStatisVOService.queyHotCommends();
        model.addAttribute("hotCds", vos);
        //abroadCds  国外推荐排行 点赞数和收藏数
        model.addAttribute("abroadCds", strategyStatisVOService.queryAbroadCommends());
        //unabroadCds  国内推荐排行
        model.addAttribute("unabroadCds", strategyStatisVOService.queryChinaCommends());
        //主题推荐  themeCds
        List<Map<String, Object>> list = strategyTemplateSearchService.queryThemeCommend();
        model.addAttribute("themeCds", list);
        //themes 主题攻略条件列表  id  name  count
        List<StatisVO> themes = strategyTemplateSearchService.queryConditionThemes();
        model.addAttribute("themes", themes);
        //chinas  国内的条件列表
        List<StatisVO> chinas = strategyTemplateSearchService.queryConditionProvinces();
        model.addAttribute("chinas", chinas);
        //abroads  国外的条件列表
        List<StatisVO> abroads = strategyTemplateSearchService.queryConditionCountries();
        model.addAttribute("abroads", abroads);
        return "strategy/index";
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        //page
        model.addAttribute("page", strategyTemplateSearchService.query(qo));
        return "strategy/searchPageTpl";
    }

    @RequestMapping("/detail")
    public String detail(Model model, Long id, @UserParam UserInfo userInfo) {
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(id));
        model.addAttribute("detail", detail);
        //只要用户进来此页面 , 那么浏览量就加1
        strategyStatisVOService.increaseViewnum(id, 1);
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(id);
        model.addAttribute("vo", vo);
        //当用户 进入此界面 , 热门攻略推荐 同样加1 加分
        strategyStatisVOService.addHotScore(id, 1);
        //用户进入此界面 , 国内前十排行榜浏览量 + 1
        strategyStatisVOService.addViewnumScoreForChinaTen(id, 1);
        //isFavor  用户是否已收藏
        if (userInfo != null) {
            model.addAttribute("isFavor", strategyStatisVOService.isFavor(id, userInfo.getId()));
        }
        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
        //tags
        model.addAttribute("tags", strategyTagService.list());
        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategy/list";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(Model model, StrategyComment strategyComment, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            //如果没有登录, 抛异常
            throw new RuntimeException("请先登录");
        }
        //缺什么 就补什么
        strategyComment.setLevel(userInfo.getLevel());
        strategyComment.setCity(userInfo.getCity());
        strategyComment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyComment.setUserId(userInfo.getId());
        strategyComment.setUsername(userInfo.getNickname());
        strategyCommentService.saveOrUpdate(strategyComment);
        //评论数的变化
        strategyStatisVOService.increaseReplynum(strategyComment.getDetailId(), 1);
        //热门推荐排行的变化 加分操作
        strategyStatisVOService.addHotScore(strategyComment.getDetailId(), 1);
        //评论数
        return AjaxResult.SUCCESS.addData(strategyStatisVOService.getStrategyStatisVO(strategyComment.getDetailId()).getReplynum());
    }

    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo) {
        model.addAttribute("page", strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }


    //攻略明细 的评论的点赞功能 toid : mongoDB 的评论集合id  fromid 指的是操作人
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid, @UserParam UserInfo userInfo) {
        if (fromid == -1L || userInfo == null || userInfo.getId() != fromid) {
            //如果没有登录或者不是同一个人 , 抛异常
            throw new RuntimeException("请先登录,再进行点赞");
        }
        strategyCommentService.thumbUp(toid, fromid);
        return AjaxResult.SUCCESS;
    }

    //收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            //如果没有登录 抛异常
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.NO_LOGIN);
            return result;
        }
        boolean ret = strategyStatisVOService.favor(sid, userInfo.getId());
        //国内外攻略排行 的 加分 如果是取消收藏 , 那么需要-1
        strategyStatisVOService.addCommendScore(sid, ret ? 1 : -1);

        return new AjaxResult(ret, "").addData(strategyStatisVOService.getStrategyStatisVO(sid));
    }

    //顶
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            //如果没有登录 抛异常
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.NO_LOGIN);
            return result;
        }
        //顶成功  ture  顶过了 false
        boolean ret = strategyStatisVOService.strategyThumbup(sid, userInfo.getId());
        if (ret) {
            //顶过了就不操作
            strategyStatisVOService.addCommendScore(sid, 1);
        }

        return new AjaxResult(ret, "").addData(strategyStatisVOService.getStrategyStatisVO(sid));
    }
}
